using MediatR;
using Medical.Domain;
using Medical.Infrastructrue.Interfaces;
using Medical.ReadApi.Dto;
using Medical.WriteApi.Appcontext.Command;
using Medical.WriteApi.Dto;
using Yitter.IdGenerator;

namespace Medical.WriteApi.Appcontext.CommandHandler
{
	public class CreateInpatientOrderTemplateCommandHandler : IRequestHandler<CreateInpatientOrderTemplateCommand, InpatientOrderTemplateDto>
	{
		private readonly IBaseRepository<InpatientOrderTemplate> _repository;

		public CreateInpatientOrderTemplateCommandHandler(IBaseRepository<InpatientOrderTemplate> repository)
		{
			_repository = repository;
		}

		public async Task<InpatientOrderTemplateDto> Handle(CreateInpatientOrderTemplateCommand request, CancellationToken cancellationToken)
		{
			var entity = new InpatientOrderTemplate
			{
				TemplateId = YitIdHelper.NextId(),
				TemplateName = request.TemplateName,
				Description = request.Description,
				IsActive = request.IsActive,
				IsDefault = request.IsDefault,
				ShowSerialNumber = request.ShowSerialNumber,
				ShowDepartment = request.ShowDepartment,
				ShowDoctor = request.ShowDoctor,
				ShowPrescription = request.ShowPrescription,
				ShowBedNumber = request.ShowBedNumber,
				ShowConsultationTime = request.ShowConsultationTime,
				ShowAdmissionTime = request.ShowAdmissionTime,
				ShowDischargeTime = request.ShowDischargeTime,
				ShowExpenseDetails = request.ShowExpenseDetails,
				ShowCashier = request.ShowCashier,
				ShowAmount = request.ShowAmount,
				CreatedAt = DateTime.Now,
				UpdatedAt = DateTime.Now
			};

			await _repository.InsertAsync(entity);

			return new InpatientOrderTemplateDto
			{
				TemplateId = entity.TemplateId,
				TemplateName = entity.TemplateName,
				Description = entity.Description,
				IsActive = entity.IsActive,
				IsDefault = entity.IsDefault,
				ShowSerialNumber = entity.ShowSerialNumber,
				ShowDepartment = entity.ShowDepartment,
				ShowDoctor = entity.ShowDoctor,
				ShowPrescription = entity.ShowPrescription,
				ShowBedNumber = entity.ShowBedNumber,
				ShowConsultationTime = entity.ShowConsultationTime,
				ShowAdmissionTime = entity.ShowAdmissionTime,
				ShowDischargeTime = entity.ShowDischargeTime,
				ShowExpenseDetails = entity.ShowExpenseDetails,
				ShowCashier = entity.ShowCashier,
				ShowAmount = entity.ShowAmount,
				CreatedAt = entity.CreatedAt,
				UpdatedAt = entity.UpdatedAt
			};
		}
	}
}
